import java.util.Scanner;

public class PlayBlackJack
{

	public void playGame()
	{
		BlackJack game = new BlackJack();
		
				
		Scanner input = new Scanner(System.in);
		
		//Card currentCard = game.dealSingleCard();
		
		
		
		//System.out.printf("Dealing %s ", currentCard.toString());
		
		//game.Shuffle();

		//game.dealFirstHand();
				
		//showHands(game);
		
		startNewGame(game);
		
		Boolean continueLooping = true;
		
		while(continueLooping)
		{
			System.out.println("\n@  Make a selection @");
			System.out.println("@    :D: to draw    @\n@    :S: to stay    @\n@    :Q: to quit    @");
			
			String response = input.next();
			
						
			if(response.equalsIgnoreCase("D"))
			{
				game.dealPlayerCard();
			}
			else if(response.equalsIgnoreCase("S"))
			{
					game.dealerDraws();
		
					showHands(game);
					
					if(game.playerTotal() > game.dealerTotal() || game.dealerTotal() > 21)
					{
						System.out.println("\n\nYou won!");
					}
					else if(game.playerTotal() == game.dealerTotal())
					{
						System.out.println("\n\nTie");
					}
					else
					{
						System.out.println("\n\nYou lost");
					}
					
					//System.out.println("Press key to continue...");
					
					//String throwAway =  input.nextLine();
					
					startNewGame(game);
						
					continue;

			}
			else if(response.equalsIgnoreCase("Q"))
			{
				continueLooping = false;
				continue;
			}
			else
			{
				System.out.println("Your input was not understood. Try again");
				continue;
			}
			
			showHands(game);

					
			if(game.playerTotal() > 21)
			{
				System.out.println("\nSorry but you got burned\n\n");
				startNewGame(game);
			}
			
			//System.out.print(response);
			
			//continueLooping = false;
			
		}
		
	// 	game.dealerDraws();
// 		
// 		showHands(game);
// 		
// 		if(game.playerTotal() > game.dealerTotal() || game.dealerTotal() > 21)
// 		{
// 			System.out.println("\n\nYou won!");
// 		}
// 		else if(game.playerTotal() == game.dealerTotal())
// 		{
// 			System.out.println("\n\nTie");
// 		}
// 		else
// 		{
// 			System.out.println("\n\nYou lost");
// 		}

		
		//game.dealDealerCards();
		

		
	}
	
	private void startNewGame(BlackJack game)
	{
	
		Scanner input = new Scanner(System.in);
		
		System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");

		System.out.println("@    Welcome to BLACKJACK     @");
		System.out.println("@    press enter to start     @");

		System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");

		input.nextLine();
					
		System.out.println("\n%  New Game   %");
					
		game.Shuffle();
		
		game.dealFirstHand();
				
		showHands(game);
	}

	
	private void showHands(BlackJack game)
	{
		int playerTotal = 0;
		
			
		System.out.println("\nYou have: \n");

		for(int i = 0; i<game.getPlayerHand().length; i++)
		{
			System.out.printf("%s\n", game.getPlayerHand()[i].toString());
			
			//playerTotal += game.getPlayerHand()[i].Rank();
		}
		if(playerTotal <= 21)
		{
			playerTotal = game.playerTotal();
			System.out.printf("\nYour total: %d", playerTotal);
		}
			else if(playerTotal > 21)
			{
				System.out.println("YOU BUST!");
			}
		
		System.out.println("\n\nDealer shows: \n");
		
		int dealerTotal = 0;
		
		for(int i = 0; i<game.getDealerHand().length; i++)
		{
			System.out.printf("%s\n", game.getDealerHand()[i].toString());
			
			//dealerTotal += game.getDealerHand()[i].Rank();
		}
		
		dealerTotal = game.dealerTotal();

		System.out.printf("\nDealer total: %d\n", dealerTotal);		
		
	// 	for(int i=0; i<game.getDealerHand().length; i++)
// 		{
// 			System.out.printf("%s\n", game.getDealerHand()[i].toString());
// 		}
	}
}